/*
* @package Nokia Places Plugin
* @title Core js file for index.php
* @author Nikolai Tihhomirov
* 
* Adds actions to buttons
* Resizes colorbox window
* Inserts place shortcode to the editor 
*
*/
jQuery( document ).ready( function(){

/**
 * input_label_action
 * 
 * @package Nokia Places Plugin
 * Adds events for search input label
 *
 */
jQuery( 'input.nokia-searchbox-input' ).keydown( function( e ){
		     switch( e.keyCode ){
                // ENTER KEY
                case 13:
                // right key
                case 39:
                   new_placelist_resize();
                   break;
                default:
                    if( this.inputTimer ){
                        clearTimeout( this.inputTimer );
                    }
                 this.inputTimer = setTimeout( function(){
				 //Check if there is at least one suggestion
				 if( jQuery( '.suggest-current' ).children().size() > 0 || jQuery( '.suggest-saved' ).children().size() > 0 || jQuery( '.suggest-nokia' ).children().size() > 0 || jQuery( '.suggest-search' ).children().size() > 0 || jQuery( '.suggest-favorites' ).children().size() > 0 )
				 {		
					//Then resize according to its height			 
				 	resize( jQuery( '.nokia-searchbox-list' ).height()-7, 0 );
				 	//Action for click on suggestion
				 	jQuery( 'span.suggestion' ).click( function(){
				 	new_placelist_resize();
				 	});
				 }
				 }, 1000 );	 
                 break;
          };
});

//Action for GO button click
jQuery( '.nokia-searchbox-button' ).click( function(){ new_placelist_resize() });

//radio buttons action
jQuery( 'input[name=group1]' ).click( function(){

	//workaround if place is not loaded yet
	if( jQuery( '#placeWidget' ).children().hasClass( 'nokia-spinner' ) ){
	return;	
	}
		var selected_template = jQuery( 'input:checked' ).val();
					//Change template in widget
					switch ( selected_template ){
					
						//Custom template
						case 'custom':
						placeWidget.setTemplate( selected_template );
						//Workaround -> default template overwrite class of placewidget
						jQuery( "#placeWidget" ).attr( 'class', 'custom-place' );
						placeWidget.setPlaceId( placeWidget.data.placeId );
						new_placewidget_resize();
						break;
						
						//Default template
						case 'default':
						default: 
						placeWidget.setTemplate( 'nokia.general.place' );
						placeWidget.setPlaceId( placeWidget.data.placeId );
						new_placewidget_resize();
						break;
				};
});

/**
 * insert_place_button_click
 * 
 * @package Nokia Places Plugin
 * adds a place to tinymce editor
 *
 */
jQuery( '.insert_place' ).click( function(){

	//workaround if place is not loaded yet
	if( jQuery( '#placeWidget' ).children().hasClass( 'nokia-spinner' ) ){ return; }
	
	//Add shortcode to editor
	insertPlacesApiLink( placeWidget.data );

});

});
//end of document ready



/**
 * colorbox.resize()
 * 
 * @package Nokia Places Plugin
 * Auto resize function
 *
 */		
function resize( y, x ){

	//Searchbox initial height 60 and width 310
	parent.jQuery.colorbox.resize( {innerHeight:60+y, innerWidth:310+x} );
return;
}

/**
 * placeList_resize()
 * 
 * @package Nokia Places Plugin
 * Resize colorbox window according to the placeList
 *
 */
function new_placelist_resize(){
	
	 setTimeout( function(){

	 	var current = jQuery( '#placeList' ).height();
	 
		//workaround if placelist is null
		if( jQuery( '#placeList' ).children().size() > 0 ){
		resize( current, 23 );	
		} else {
		//resize back to searchbox
		resize( -10, 0 );
		}
		
		//Action for pagination
		jQuery( 'ul.list-pagination > li' ).click( function(){
			//Sometimes next page placelist is different height so resize
			if( current != jQuery( '#placeList' ).height() ){
				new_placelist_resize();
				}
		
		});
	}, 1000 );
return;
}

/**
 * placewidget_resize()
 * 
 * @package Nokia Places Plugin
 * Resize colorbox window according to the placewidget
 *
 */
function new_placewidget_resize(){
	setTimeout( function(){
		//Compare widget and list heights
		if( jQuery( '#placeWidget' ).height() > jQuery('#placeList').height()){
			//Calculate height according to <td> widget
			var x = jQuery( '#widget_place' ).height()-50;
			}else{
			//Calculate height accorditng to placelist height
			var x = jQuery( '#placeList' ).height();
			}
		//Resize now
		parent.jQuery.colorbox.resize( {innerHeight:60+x, innerWidth:348+jQuery( '#placeWidget' ).width()} )
		
	}, 1000 );
return;
}

/**
 * format_place_shortcode
 * 
 * @package Nokia Places Plugin
 * Creates a shortcode for particular place and puts it into tinymce content
 *
 */
function insertPlacesApiLink( data ) {
	var tagtext;
	var win = window.dialogArguments || opener || parent || top;
	var selected_template = jQuery( 'input:checked' ).val();
		if ( data.placeId != '' ){
			tagtext = '[nokia template="'+selected_template+'" place="'+data.placeId+'"] ';
		}else{
			win.jQuery.colorbox.close();
		}
//Tinymce stuff
var ed;		
if( typeof win.tinyMCE != 'undefined' && ( ed = win.tinyMCE.activeEditor ) && !ed.isHidden()  ) {
		//WYSIWYG
		ed.execCommand( 'mceInsertContent', false, tagtext );
		//Peforms a clean up of the current editor HTML. 
		ed.execCommand( 'mceCleanup' );
		//Repaints the editor. Sometimes the browser has graphic glitches. 
		ed.execCommand( 'mceRepaint' );
	} else if ( typeof win.edInsertContent == 'function' ) {
		//html tab
		win.edInsertContent( win.edCanvas, tagtext );
	} else {
		//dashboard
		jQuery( win.edCanvas ).val( jQuery( win.edCanvas ).val() + tagtext );
	}
	//Close window
	win.jQuery.colorbox.close();
return;
}